- Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathPalindromePartitioning.java
169 lines (124 loc) Β· 4.31 KB
/
PalindromePartitioning.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
packagesection19_DynamicProgramming;
/*
* Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s. [Leetcode 132]
*/
importjava.util.Arrays;
publicclassPalindromePartitioning {
publicstaticvoidmain(String[] args) {
Stringsource = "abacdc";
intstartIndex = 0, endIndex = source.length() - 1;
System.out.println(palindromePartitionRec(source, startIndex, endIndex));
Stringsrc = "abacdce";
System.out.println(palindromePartitionRec(src, 0, src.length() - 1));
int[][] strg = newint[src.length()][src.length()];
for (inti = 0; i < strg.length; i++) {
Arrays.fill(strg[i], -1);
}
System.out.println("using top down for small input");
System.out.println(palindromePartitioningTopDownDP(src, 0, src.length() - 1, strg));
System.out.println("\ntesting large inputs");
Strings = "aeirahsdvmzxklncaodgnasadasdsafhioashdffoihokn";
// System.out.println(palindromePartitionRec(s, 0, s.length() - 1));
int[][] storage = newint[s.length()][s.length()];
for (inti = 0; i < storage.length; i++) {
Arrays.fill(storage[i], -1);
}
System.out.println(palindromePartitioningTopDownDP(s, 0, s.length() - 1, storage));
System.out.println("\nusing bottom up dp");
System.out.println(palindromePartitioningBottomUpDP("aba")); // 0
System.out.println(palindromePartitioningBottomUpDP(source)); // 1
System.out.println(palindromePartitioningBottomUpDP("abacdce")); // 2
System.out.println(palindromePartitioningBottomUpDP(s)); // 38
}
publicstaticintpalindromePartitionRec(Stringstr, intsi, intei) {
if (isPalindrome(str, si, ei)) {
return0;
}
intminCut = Integer.MAX_VALUE;
for (intcalls = si; calls < ei; calls++) {
intfirstHalf = palindromePartitionRec(str, si, calls);
intsecondHalf = palindromePartitionRec(str, calls + 1, ei);
intans = firstHalf + secondHalf + 1;
if (ans < minCut) {
minCut = ans;
}
}
returnminCut;
}
publicstaticbooleanisPalindrome(Stringstr, intsi, intei) {
intlow = si, high = ei;
while (low < high) {
if (str.charAt(low) != str.charAt(high))
returnfalse;
low++;
high--;
}
returntrue;
}
publicstaticintpalindromePartitioningTopDownDP(Stringstr, intsi, intei, int[][] storage) {
if (isPalindrome(str, si, ei)) {
return0;
}
intminCut = Integer.MAX_VALUE;
if (storage[si][ei] != -1) { // reuse
returnstorage[si][ei];
}
for (intcalls = si; calls < ei; calls++) {
intfirstHalf = palindromePartitioningTopDownDP(str, si, calls, storage);
intsecondHalf = palindromePartitioningTopDownDP(str, calls + 1, ei, storage);
intans = firstHalf + secondHalf + 1;
if (ans < minCut) {
minCut = ans;
}
}
storage[si][ei] = minCut; // store solution in problem index
returnminCut;
}
publicstaticintpalindromePartitioningBottomUpDP(Stringstr) {
boolean[][] palindromeStorageReuse = palindromeStorage(str);
intlen = str.length();
int[][] storage = newint[len][len];
// filling direction
for (intslide = 0; slide <= len - 1; slide++) {
for (intsi = 0; si <= len - slide - 1; si++) {
intei = si + slide;
// logic from top down
// optimizing this task to check palindrome for each cell
if (palindromeStorageReuse[si][ei]) {
storage[si][ei] = 0;
} else {
intminCut = Integer.MAX_VALUE;
for (intcalls = si; calls < ei; calls++) {
intfirstHalf = storage[si][calls];
intsecondHalf = storage[calls + 1][ei];
intans = firstHalf + secondHalf + 1;
if (ans < minCut) {
minCut = ans;
}
}
storage[si][ei] = minCut; // store solution in problem index
}
}
}
returnstorage[0][len - 1];
}
// optimizing base case to check palindrome for each cell
publicstaticboolean[][] palindromeStorage(Stringstr) {
intn = str.length();
boolean[][] palindromeInfo = newboolean[n][n];
for (inti = 0; i < palindromeInfo.length; i++) {
Arrays.fill(palindromeInfo[i], true);
}
for (introw = n - 2; row >= 0; row--) {
for (intcol = row + 1; col < n; col++) {
if (str.charAt(row) == str.charAt(col)) {
palindromeInfo[row][col] = palindromeInfo[row + 1][col - 1];
} else {
palindromeInfo[row][col] = false;
}
}
}
returnpalindromeInfo;
}
}